package com.jwaoo.mobile.forchat;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.dx.as.JWCache;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.FragmentUtils;
import com.dx.as.common.string.StringUtil;
import com.dx.as.listener.IForChatModule;
import com.jwaoo.chat.ChatHelpter;
import com.jwaoo.mobile.ApplicationHelper;
import com.jwaoo.mobile.http.HttpHelper;
import com.jwaoo.mobile.view.login.LoginActivity;
import com.jwaoo.mobile.view.main.MainActivity;
import com.jwaoo.mobile.view.user.UserActivity;
import java.util.List;
import http.IHttpListener;

/**
 * Created by jwaoo on 2017/8/2.
 */

public class IFroChatMouleImpl extends IForChatModule{

    private static IFroChatMouleImpl iFroChatMoule;

    private IFroChatMouleImpl(){
    }

    public static IFroChatMouleImpl getInstance(){
        if (iFroChatMoule == null){
            synchronized (IFroChatMouleImpl.class){
                if (iFroChatMoule == null){
                    iFroChatMoule = new IFroChatMouleImpl();
                }
            }
        }
        return iFroChatMoule;
    }

    public void initChatModule(){
        ChatHelpter.init(getConext(),iFroChatMoule);
        loginChatSystem();
    }

    @Override
    public void gotoUserActivity(int friend) {
        Intent intent = new Intent(JWCache.getContext(), UserActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("id",friend);
        JWCache.getContext().startActivity(intent);
    }

    @Override
    public Activity getMainContext() {
        return MainActivity.getActivity();
    }

    @Override
    public String getToken() {
        return CommonUtils.getToken();
    }

    @Override
    public String getChatUrl() {
        return CommonUtils.getChatUrl();
    }

    @Override
    public String getRTCUrl() {
        return CommonUtils.getRtcUrl();
    }

    @Override
    public String getIMName() {
        return CommonUtils.getIMName();
    }

    @Override
    public int getIMAcount() {
        return CommonUtils.getIMAccount();
    }

    @Override
    public void loginChatSystem() {
        if (StringUtil.isEmpty(getToken()) || StringUtil.isEmpty(getChatUrl())) return;
        getiOptionFromChat().loginChatSystem();
    }

    @Override
    public void stopChatSystem() {
        getiOptionFromChat().stopChatSystem();
    }

    @Override
    public void closeChatDb() {
        getiOptionFromChat().closeChatDb();
    }

    @Override
    public void startChatting(int friend) {
        getiOptionFromChat().startChatting(friend);
    }

    @Override
    public void getContactAsy(IHttpListener listener, int uuid) {
        HttpHelper.getUserInfo(listener,uuid);
    }

    @Override
    public void updateUnReadCount(int count,boolean isByDelete) {
        onUnReadChangeListener.onUnReadCountChanged(count,isByDelete);
    }

    @Override
    public void onKickedOffline() {
        ActivityManager activityManager = (ActivityManager)JWCache.getContext().getSystemService(Context.ACTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();
            for (ActivityManager.AppTask appTask : appTaskList){
                appTask.finishAndRemoveTask();
            }
        }
        ApplicationHelper.lastCls = null;
        Intent intent = new Intent(JWCache.getContext(), LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(JWCache.EXTRA_ONKICKED_OFF_LINE,true);
        JWCache.getContext().startActivity(intent);
    }

    @Override
    public Context getConext() {
        return JWCache.getContext();
    }

    public void setUnReadCountChangedListener(OnUnReadChangeListener listener){
        onUnReadChangeListener = listener;
    }

    private OnUnReadChangeListener onUnReadChangeListener;
    public interface OnUnReadChangeListener{
        void onUnReadCountChanged(int count,boolean isByDelete);
    }

    @Override
    public boolean isCurrentActivity(Class<?> cls) {
        return ApplicationHelper.isCurentActvity(cls);
    }

    @Override
    public void savePushMsg(String msgData, long time ,int friend) {
        getiOptionFromChat().savePushMsg(msgData,time,friend);
    }

    @Override
    public boolean isBackground() {
        return ApplicationHelper.isBackground();
    }

    public void updateAccount(){
        stopChatSystem();
        closeChatDb();
        FragmentUtils.clearFragment();
        CommonUtils.clearAccountInfo();
    }

    @Override
    public Class<?> getCurrentActivity() {
        return ApplicationHelper.getCurrentActivity();
    }
}
